package com.ewei.support.javax.mail;

import javax.mail.AuthenticationFailedException;
import javax.mail.Folder;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.URLName;

import org.budo.support.slf4j.Slf4j;
import org.slf4j.Logger;

import org.budo.mail.exception.AuthenticationFailedRuntimeException;

/**
 * @author li
 * @version 1 (2015年2月12日 下午1:08:38)
 * @since Java7
 */
public class StoreUtil {
    private static final Logger log = Slf4j.getLogger();

    public static Store connect(Session session, String protocol, String host, Integer port, String username, String password) {
        Store store = null;
        try {
            URLName urlName = new URLName(protocol, host, port, null /* String file */, username, password);
            // session.setDebug(true);
            store = session.getStore(urlName);
            store.connect();
            return store;
        } catch (AuthenticationFailedException e) {
            StoreUtil.closeStore(store);
            throw new AuthenticationFailedRuntimeException("#33 session=" + session //
                    + ", protocol=" + protocol //
                    + ", host=" + host //
                    + ", port=" + port //
                    + ", username=" + username //
                    + ", e=" + e, e);
        } catch (Throwable e) {
            StoreUtil.closeStore(store);
            String message = "#41 " + e + ", session=" + session //
                    + ", protocol=" + protocol //
                    + ", host=" + host //
                    + ", port=" + port //
                    + ", username=" + username;
            throw new RuntimeException(message, e);
        }
    }

    public static void closeStore(Store store) {
        if (store != null) {
            try {
                store.close();
            } catch (MessagingException e) {
                log.error("closeStore error, " + e, e);
            }
        }
    }

    public static Store getStore(String protocol, String host, String email, String password, Boolean isSsl, Integer port) throws Exception {
        Store store = null;
        try {
            Session session = MailSessionUtil.getSession(protocol, host, isSsl, port);
            store = session.getStore(protocol);
        } catch (Exception e) {
            log.error("getStore error " + e, e);
            StoreUtil.closeStore(store);
            throw e;
        }
        return store;
    }

    public static Store connect(Store store, String username, String password) {
        try {
            store.connect(username, password);
            return store;
        } catch (MessagingException e) {
            StoreUtil.closeStore(store);
            throw new RuntimeException(e);
        }
    }

    public static void closeInbox(Folder inbox, Boolean deleteMails) {
        if (inbox != null) {
            try {
                if (inbox.isOpen()) {
                    inbox.close(deleteMails);
                } else {
                    log.warn("closeInbox error, inbox is not open, inbox=" + inbox + ", deleteMails=" + deleteMails);
                }
            } catch (MessagingException e) {
                log.error("closeInbox error, " + e, e);
            }
        }
    }
}